home *** CD-ROM | disk | FTP | other *** search
/ Aminet 52 / Aminet 52 (2002)(GTI - Schatztruhe)[!][Dec 2002].iso / Aminet / dev / lang / AmigaTalk.lha / general / KeyedCollection.st < prev    next >
Text File  |  2002-01-29  |  3KB  |  91 lines

  1. Class KeyedCollection :Collection
  2. [
  3.    add: anElement
  4.       ^ self error: 'Must add with explicit key'
  5. |
  6.    addAll: aCollection
  7.       aCollection binaryDo: [:x :y | self at: x put: y].
  8.       ^ aCollection
  9. |
  10.    asDictionary        ! newCollection !
  11.       newCollection <- Dictionary new.
  12.       self binaryDo: 
  13.          [:key :val ! newCollection at: key put: val].
  14.  
  15.       ^ newCollection
  16. |
  17.    at: key
  18.       ^ self at: key ifAbsent:
  19.                  [self error:
  20.                      (key printString , ': association not found').
  21.                   
  22.                   ^ key
  23.                  ]
  24. |
  25.    atAll: aCollection put: anObject
  26.       aCollection do: [:x ! self at: x put: anObject]
  27. |
  28.    binaryDo: aBlock  ! item !
  29.       self do: [:x | aBlock value: self currentKey value: x ].
  30.       ^ nil
  31. |
  32.    collect: aBlock 
  33.       ^ self coerce:
  34.            (self inject: Dictionary new
  35.                    into: [:x :y ! x at: self currentKey
  36.                                    put: (aBlock value: y) . x ] )
  37. |
  38.    includesKey: key
  39.       self at: key ifAbsent: [^ false].
  40.       ^ true
  41. |
  42.    indexOf: anElement
  43.       ^ self indexOf: anElement
  44.       ifAbsent: [self error: 'indexOf element not found']
  45. |
  46.    indexOf: anElement ifAbsent: exceptionBlock
  47.       self do: [:x | (x = anElement) 
  48.                 ifTrue: [ ^ self currentKey ]].
  49.       ^ exceptionBlock value
  50. |
  51.    keys ! newset !
  52.       newset <- Set new.
  53.       self keysDo: [:x | newset add: x].
  54.       ^ newset
  55. |
  56.    keysDo: aBlock
  57.       ^ self do: [ :x | aBlock value: self currentKey ]
  58. |
  59.    keysSelect: aBlock          
  60.       ^ self coerce:
  61.            (self inject: Dictionary new
  62.                    into: [:x :y | (aBlock value: y currentKey)
  63.                                   ifTrue: [x at: self currentKey
  64.                                             put: y]. x ] )
  65. |
  66.    remove: anElement
  67.       ^ self error: 'object must be removed with explicit key'
  68. |
  69.    removeKey: key
  70.       ^ self removeKey: key ifAbsent:
  71.              [self error: 'no element associated with key'. ^ key]
  72. |
  73.    removeKey: key ifAbsent: exceptionBlock
  74.       ^ self error: 'subclass should implement RemoveKey:ifAbsent:'
  75. |
  76.    select: aBlock          
  77.       ^ self coerce:
  78.            (self inject: Dictionary 
  79.                    into: [:x :y | (aBlock value: y)
  80.                                    ifTrue: [x at: self currentKey put: y]. 
  81.                                   x 
  82.                          ] )
  83. |
  84.    values ! newbag !
  85.       newbag <- Bag new.
  86.  
  87.       self do: [:x | newbag add: x].
  88.  
  89.       ^ newbag
  90. ]
  91.